package com.etheart.music.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etheart.framework.core.dao.CrudService;
import com.etheart.framework.core.dao.QueryService;
import com.etheart.model.entity.Resource;
import com.etheart.model.entity.Song;

@Service("music_ResourceService")
public class ResourceServiceImp implements ResourceService{
	@Autowired
	@Qualifier("etheart_QueryService")
	private QueryService queryService;

	@Autowired 
	@Qualifier("etheart_CrudService")
	private CrudService crudService;
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#findAll()
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Resource> findAll(){
		return queryService.findAll(Resource.class);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#addSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void addResource(Resource resource){
		crudService.create(resource);
	}
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#updateSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void updateResource(Resource resource){
		crudService.update(resource);
	}
	
	/* (non-Javadoc)
	 * @see com.etheart.lyric.music.service.SongService#deleteSong(com.etheart.module.entity.Song)
	 */
	@Override
	@Transactional
	public void deleteResource(Resource resource){
		crudService.delete(resource);
	}

	@Override
	public Resource findById(Long id) {
		return queryService.findById(Resource.class, id);
	}

	@Override
	public Resource findByNetId(String netId) {
		DetachedCriteria criteria=DetachedCriteria.forClass(Resource.class).add(Restrictions.eq("netId", netId+""));
		List<Resource> resources=queryService.findListByDetachedCriteria(criteria);
		if (resources.size()>0) {
			return resources.get(0);
		}
		return null;
	}



	
}
